package com.jinlejia.erp.ui.common;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.freedom.yefeng.yfrecyclerview.FullyLinearLayoutManager;
import com.freedom.yefeng.yfrecyclerview.YfListRecyclerView;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.DialogSelectAdapter;

import java.util.ArrayList;


/**
 * 选择 Dialog
 */
public class SelectDialog extends AlertDialog implements View.OnClickListener {

    private String titleStr;
    private ArrayList<String> data;
    public DialogSelectAdapter adapter;

    public SelectDialog(Context context, String title, ArrayList<String> datalist) {
        super(context);
        this.titleStr = title;
        this.data = datalist;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
        window.setContentView(R.layout.dialog_select);
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawable(new ColorDrawable());
        if( data.size()<4 ){
            window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT );
        }else{
            window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, 600);
        }

        //取消
        TextView cancle = (TextView) window.findViewById(R.id.cancel_btn);
        //标题
        TextView title = (TextView) window.findViewById(R.id.title_text);
        title.setText(titleStr);
        YfListRecyclerView recyclerView = (YfListRecyclerView) window.findViewById(R.id.recycler_view);
//        recyclerView.setLayoutManager(new UnfoldLinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
        recyclerView.setLayoutManager(new FullyLinearLayoutManager(getContext(), LinearLayout.VERTICAL,false));
        recyclerView.setHasFixedSize(true);
        adapter = new DialogSelectAdapter(data);
        recyclerView.setAdapter(adapter);
        cancle.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel_btn:
                dismiss();
                break;
            default:
                break;
        }
    }


}
